﻿using MediatR;
using RBAC_Uthorization.Domain.Entity;
using RBAC_Uthorization.Infrstructre.Interface;
using RBAC_Uthorization.Read.API.Application.Command.SysCommand;
using RBAC_Uthorization.Read.API.DTO;

namespace RBAC_Uthorization.Read.API.Application.CommandHandlers.SysHanlder
{
    public class SysShowCommandHandlers : IRequestHandler<SysShowCommand, ApiResult<List<sys_System>>>
    {
        private readonly ISysRepository _sysRepository;
        private readonly ILogger<SysShowCommandHandlers> _logger;

        public SysShowCommandHandlers(ISysRepository sysRepository, ILogger<SysShowCommandHandlers> logger)
        {
            _sysRepository = sysRepository;
            _logger = logger;
        }

        public async Task<ApiResult<List<sys_System>>> Handle(SysShowCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var sysList = await _sysRepository.GetAllAsync();
                return new ApiResult<List<sys_System>>() { Data= sysList };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取系统列表时发生错误");
                return null;
            }
        }
    }
}
